package com.example.administrator.pomelostreet.fragment;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.pomelostreet.HttpUtils.HttpUtil;
import com.example.administrator.pomelostreet.HttpUtils.Item_list;
import com.example.administrator.pomelostreet.HttpUtils.NewClothFragmentJson1;
import com.example.administrator.pomelostreet.HttpUtils.NewClothFragmentJson1Been;
import com.example.administrator.pomelostreet.HttpUtils.NewClothesFragmentJson;
import com.example.administrator.pomelostreet.HttpUtils.NewClothesFragmentbeen;
import com.example.administrator.pomelostreet.R;
import com.example.administrator.pomelostreet.adapter.MyBaseadapter;
import com.example.administrator.pomelostreet.asynctask.Myasynctaski;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import static android.R.id.list;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import static java.security.AccessController.getContext;


/**
 * A simple {@link Fragment} subclass.
 */
public class NewClothesFragment extends Fragment {
    private List<NewClothesFragmentbeen> getjson;
    private String path = "http://api.youzibuy.com/tae_channel_market?myuid=129829333&tbuid=&device_id=867323021263676&platform=android&v=2.1.0&imei=867323021263676&bundleid=186&mode=0&app_id=7&v_auth=3.Tw3XuxZufEJE1O3YB%2FCFwfeQpjfJqyupPY3ADa6%2BUC0%3D&statinfo=eyJzb3VyY2UiOiItPiIsImNoYW5uZWxpZCI6IjE4NiIsIm9zIjoiMiIsIm90Ijoi5Lit5Zu956e75YqoIiwiYXBuIjoiNCIsInVhIjoiTUkgNExURSIsIm9zdmVyc2lvbiI6IjYuMC4xIiwic2RrdmVyc2lvbiI6MjMsInYiOiIyLjEuMCIsImJ1aWxkdiI6IjIuMS4wIiwidWlkIjoxMjk4MjkzMzMsIm9wZW51ZGlkIjoiOTBlYTZiMzMtNTEzNi00YWRjLTg2MjctMTljZTdhYmM4Mzg3IiwibWFjIjoiODY3MzIzMDIxMjYzNjc2IiwiYW5kcm9pZGlkIjoiMmYxMWIxZGU4MTI2NTJmNyIsImltZWkiOiI4NjczMjMwMjEyNjM2NzYiLCJpbXNpIjoiNDYwMDc1MDEwMzU0OTk4In0=&myclient=0730210018600000&sdkversion=23&channel_id=1&platform=android&v=2.1.0&device_id=867323021263676";
    private String str = "http://api.youzibuy.com/tae_brand_list?myuid=130770451&tbuid=&device_id=02:00:00:00:00:00&platform=android&v=1.3.2&imei=861483030821249&bundleid=8&mode=0&app_id=7&v_auth=3.Sw7GJwyGIrVuebGqQWmMDO05GtvBjkYCHYMCWwYoIsg%253D&sdkversion=23&path=&timestamp=";
    MyHandler handler;
//    private Banner banner;
    private Banner banner;
    private GridView gv;
   // private RecyclerView recyclerView;
   XRecyclerView recyclerView;
    List<Item_list> lists=new ArrayList<>();
    MyBaseadapter baseadapter;

    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    banner.setImages(getjson);
                    banner.start();
                    break;
                case 2:
                    List<NewClothFragmentJson1Been> arr = (List<NewClothFragmentJson1Been>) msg.obj;
                    Log.i("Tag", arr.size() + "");
                    gv.setAdapter(new MyAdapter(getContext(), arr));
                    break;
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_new_clothes, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerView = (XRecyclerView) view.findViewById(R.id.recycler_view);

         baseadapter=new MyBaseadapter(lists,getContext());
        LinearLayoutManager layoutManager=new LinearLayoutManager(getContext());
        //      recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(layoutManager);

        //加上recyclerView的头部视图

        View   header2 = LayoutInflater.from(getContext()).inflate(R.layout.xrecyclerview_header, null);
        banner = (Banner) header2.findViewById(R.id.banner);
        gv = (GridView) header2.findViewById(R.id.gridView);

        recyclerView.addHeaderView(header2);

        recyclerView.setAdapter(baseadapter);


       // banner = (Banner) view.findViewById(R.id.banner);


//
//         banner.setImageLoader(new GlideImageLoader());
//           banner.setImages(json);
        Log.i("aaa","======");
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                NewClothesFragmentbeen bean = (NewClothesFragmentbeen) path;
                Glide.with(getContext()).load(bean.getPicture_url()).placeholder(R.mipmap.ic_launcher).into(imageView);
            }
        });







        Myasynctaski asynctaski=new Myasynctaski(getContext(), new Myasynctaski.youziCallBack() {
            @Override
            public void send(List<Item_list> list) {
                if (list!=null&&list.size()>0){
                    lists.addAll(list);
                    baseadapter.notifyDataSetChanged();
                }


            }
        });
        asynctaski.execute(str);

        getData();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        handler = new MyHandler();
    }

    //    public class GlideImageLoader extends ImageLoader {
//
//        @Override
//        public void displayImage(Context context, Object path, ImageView imageView) {
//            Glide.with(context).load(path).into(imageView);
//        }
//    }

    public void getData() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                byte[] data = HttpUtil.getDataUseOkHttp(path);
                Log.i("--->", data + "");
                String json = new String(data);
                Message msg = new Message();
                getjson = NewClothesFragmentJson.getjson(json);
                Log.i("TAG", "run: " + getjson);
                msg.what = 1;
                handler.sendMessage(msg);


                Message msg2 = new Message();
                msg2.what = 2;
                List<NewClothFragmentJson1Been> getresult = NewClothFragmentJson1.getresult(json);
                msg2.obj = getresult;
                handler.sendMessage(msg2);


            }
        }.start();


    }

    //设置GridView的适配器
    class MyAdapter extends BaseAdapter {
        private Context context;
        private List<NewClothFragmentJson1Been> arr;
        NewClothFragmentJson1Been next1;
        View convertView;
        ViewHolder viewHolder;

        public MyAdapter(Context context, List<NewClothFragmentJson1Been> arr) {
            this.context = context;
            this.arr = arr;
        }

        @Override
        public int getCount() {
            return arr.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.fragment_new_clothes_base, parent, false);
                viewHolder = new ViewHolder();

                viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView_fragment_new_clothes);
                viewHolder.textView = (TextView) convertView.findViewById(R.id.textView_fragment_new_clothes);

//                imageView=new ImageView(context);
//                imageView.setLayoutParams(new GridView.LayoutParams(95,95));
//
//                textView=new TextView(context);
//               textView.setLayoutParams(new GridView.LayoutParams(15,15));

                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();

            }
            NewClothFragmentJson1Been json1Been = arr.get(position);
            String url = json1Been.getPicture_url();
            String name = json1Been.getName();
//            textView.setText(name);
//            Iterator<NewClothFragmentJson1Been> iterator = arr.iterator();
//            while (iterator.hasNext()){
//                 next1 = iterator.next();
//                Glide.with(context).load(next1.getPicture_url()).into(imageView);
//            }
            //  textView.setText(next1.getName().toString());
            Glide.with(context).load(url).into(viewHolder.imageView);
            viewHolder.textView.setText(name);
            return convertView;
        }

        class ViewHolder {
            ImageView imageView;
            TextView textView;

        }

    }


}

